 package GEMonitor;

 public class Helper
 {
     //若某字节为负数则需将其转成无符号正数
     public static int transform(byte temp){
        int tempInt = (int)temp;
        if(tempInt < 0){
            tempInt += 256;
         }
         return tempInt;
     }

     public static int[] byteToInt(byte[]a)
     {
        int len = a.length;
        if(len == 0)
        {
            return null;
        }
        int[] arr = new int[len];
        for(int i=0; i<len; i++)
        {
            arr[i] = transform(a[i]);
        }
        return arr;
     }

    public static String checksum(byte[] a)
    {

        int[] arr = byteToInt(a);

        int sum = 0;
        int len = arr.length;
        for(int i=0; i<len; i++)
        {
            sum += arr[i];
        }

        while (sum > 255)
        {
            sum = (sum >> 8) + (sum & 0xff);
        }

        String slen = String.format("%02X", sum);

//        if(sum == 256)
//        {
//            slen = "01";
//        }

        return slen;
    }

    public static String randomString(int n)
    {
        String chars = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMabcdefghijklmnopqrstuvwxyz";
		StringBuffer value = new StringBuffer();
		for (int i = 0; i < n; i++) {
			value.append(chars.charAt((int)(Math.random() * 61)));
        }
        return value.toString();
    }

//    public static byte[] TEA32(byte[] content, String keySeed)
//    {
//        if(keySeed.length() != 12)
//        {
//            return null;
//        }
//
//        int[] keybuf = getKey(keySeed);
//
//        //byte[] arr = TeaUtil.encryptByTea(content, keybuf);
//        byte[] arr = TeaUtil.encrypt(content, 0, keybuf, 32);
//        return arr;
//    }

    public static int[] getKey(String keySeed)
    {
        byte[] arrTmp = keySeed.getBytes();

        byte[] keyTemp = new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0x63,0x6d,0x6c,0x63};
        for(int i=0; i<12; i++)
        {
            keyTemp[i] = arrTmp[i];
        }
        byte x = keyTemp[10];
        keyTemp[10] = keyTemp[14];
        keyTemp[14] = x;

        int[] result = new int[]{0,0,0,0};
        result[0] += keyTemp[0];
        result[0] += keyTemp[1];
        result[0] += keyTemp[2];
        result[0] += keyTemp[3];

        result[1] += keyTemp[4];
        result[1] += keyTemp[5];
        result[1] += keyTemp[6];
        result[1] += keyTemp[7];

        result[2] += keyTemp[8];
        result[2] += keyTemp[9];
        result[2] += keyTemp[10];
        result[2] += keyTemp[11];

        result[3] += keyTemp[12];
        result[3] += keyTemp[13];
        result[3] += keyTemp[14];
        result[3] += keyTemp[15];

        return result;
    }

    public static String byteArrayToHexStr(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[byteArray.length * 2];
        for (int j = 0; j < byteArray.length; j++) {
            int v = byteArray[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static byte[] hexStrToByteArray(String str) {
        if (str == null) {
            return null;
        }
        if (str.length() == 0) {
            return new byte[0];
        }
        byte[] byteArray = new byte[str.length() / 2];
        for (int i = 0; i < byteArray.length; i++) {
            String subStr = str.substring(2 * i, 2 * i + 2);
            byteArray[i] = ((byte) Integer.parseInt(subStr, 16));
        }
        return byteArray;
    }       
 }